package top.myhdg.bijou;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.ArrayList;

import top.myhdg.bijou.part.MaxHListView;

public class MultiDialog extends Dialog {
    private Context context;

    private MaxHListView pageList;

    MultiDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //==========设置工具箱Dialog位置及动画==========
        Window window = getWindow();
        assert window != null;
        window.setGravity(Gravity.BOTTOM); // 设置dialog显示位置
        window.setWindowAnimations(R.style.slide_animation); // 添加动画效果
        setContentView(R.layout.dialog_multi);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = displayMetrics.widthPixels; // 设置dialog宽度
        getWindow().setAttributes(layoutParams);
        setCanceledOnTouchOutside(true);// 点击Dialog外部消失
        //==========设置工具箱Dialog位置及动画==========

        pageList = findViewById(R.id.listViewPage);
        pageList.setListViewHeight(MaxHListView.dpToPx(pageList.getContext(), 220));//设置最大高度
        pageList.setVerticalScrollBarEnabled(false);//隐藏普通滚动条
        pageList.setFastScrollEnabled(false);//隐藏快速滚动条

        Button buttonDismiss = findViewById(R.id.buttonDismissMulti);
        buttonDismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    void setPageAdapter(PageAdapter pageAdapter){
        pageList.setAdapter(pageAdapter);
    }

    //==========设置各个按钮监听事件==========
    void setAddPageClickListener(View.OnClickListener onClickListener) {
        ImageButton addPage = findViewById(R.id.addPage);
        addPage.setOnClickListener(onClickListener);
    }

    void setIncognitoClickListener(View.OnClickListener onClickListener) {
        ImageButton incognito = findViewById(R.id.buttonIncognito);
        incognito.setOnClickListener(onClickListener);
    }

    void setEmptyClickListener(View.OnClickListener onClickListener) {
        ImageButton emptyPage = findViewById(R.id.emptyPage);
        emptyPage.setOnClickListener(onClickListener);
    }

    void setPageListItemListener(AdapterView.OnItemClickListener onItemClickListener) {
        pageList.setOnItemClickListener(onItemClickListener);
    }
    //==========设置各个按钮监听事件==========

}

//==========多页切换数据相关==========
class PageItem {
    private int pageId;
    private String pageTitle;
    private String pageWebsite;
    private View.OnClickListener onClickListener;

    PageItem(int pageId, String pageTitle, String pageWebsite, View.OnClickListener onClickListener) {
        this.pageId = pageId;
        this.pageTitle = pageTitle;
        this.pageWebsite = pageWebsite;
        this.onClickListener = onClickListener;
    }

    int getPageId() {
        return pageId;
    }

    String getPageTitle() {
        return pageTitle;
    }

    String getPageWebsite() {
        return pageWebsite;
    }

    View.OnClickListener getOnClickListener() {
        return onClickListener;
    }
}

class PageAdapter extends ArrayAdapter<PageItem> {
    private int textViewResourceId;

    PageAdapter(@NonNull Context context, int textViewResourceId, ArrayList<PageItem> objects) {
        super(context, textViewResourceId, objects);
        this.textViewResourceId = textViewResourceId;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        PageItem pageItem = getItem(position);
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(textViewResourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.pageTitle = view.findViewById(R.id.pageTitle);
            viewHolder.pageSite = view.findViewById(R.id.pageWebsite);
            viewHolder.deletePage = view.findViewById(R.id.imageButtonDelete);
            view.setTag(viewHolder); // 将ViewHolder存储在View中
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag(); // 重新获取ViewHolder
        }
        assert pageItem != null;
        int pageId = pageItem.getPageId() + 1;
        try {
            String pageTitle = pageItem.getPageTitle();
            if (pageItem.getPageWebsite().equals("about:blank")) {
                pageTitle = "BIJOU主页";
            }
            pageTitle = pageId + ". " + pageTitle;
            viewHolder.pageTitle.setText(pageTitle);
            viewHolder.pageSite.setText(pageItem.getPageWebsite());
        } catch (Exception e) {
            e.printStackTrace();
        }
        viewHolder.deletePage.setOnClickListener(pageItem.getOnClickListener());
        return view;
    }

    class ViewHolder {
        TextView pageTitle;
        TextView pageSite;
        ImageButton deletePage;
    }
}

//==========多页切换数据相关==========